﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SharpIM.Http
{
    public class ResponseStream : Stream
    {
        /// <summary>
        /// 确认流可读
        /// </summary>
        public void EnsureValid()
        {
            if (!this.CanRead)
            {
                throw new InvalidOperationException("stream is unreadable.");
            }
        }
        Stream sourceStream;
        public ResponseStream(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                sourceStream = new MemoryStream();
            }
            sourceStream = new MemoryStream(Encoding.UTF8.GetBytes(value));
        }
        public ResponseStream(Stream value)
        {
            sourceStream = value;
        }

        public override bool CanRead
        {
            get
            {
                return sourceStream.CanRead;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return sourceStream.CanSeek;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return sourceStream.CanWrite;
            }
        }

        public override long Length
        {
            get
            {
                return sourceStream.Length;
            }
        }

        public override long Position
        {
            get
            {
                return sourceStream.Position;
            }

            set
            {
                sourceStream.Position = value; ;
            }
        }

        public override void Flush()
        {
            sourceStream.Flush();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return sourceStream.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return sourceStream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            sourceStream.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            sourceStream.Write(buffer, offset, count);
        }

        protected override void Dispose(bool disposing)
        {
            this.sourceStream.Dispose();
            base.Dispose(disposing);
        }
    }
}
